<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <h1>用数组实现斐波那契数列</h1>
    <label for="text1">输入</label>
    <input type="text" id="text1">
    <input type="button" value="结果" id="btn">
    <div id="result"></div>
</body>
<script>
    (function () {
        var text1 = document.getElementById('text1');
        var btn = document.getElementById('btn');
        var result = document.getElementById('result');
        btn.onclick = function () {
            var a = [];
            var n = text1.value;

            for (var i = 0; i < n; i++) {
                if (i < 2) {
                    a.push(1);
                } else {
                    var c = a[i - 1] + a[i - 2]
                    a.push(c);
                }
            }
            result.innerHTML = a;
        }
    })();
</script>

</html>